package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.trade.CrossUnionTradeMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_UNION_TRADE_ROUTE_LIST_DATA, desc = "路线信息")
public class CrossGetTradeRouteListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();


        UnionTradeProto.RouteListRespMsg.Builder respMsg = UnionTradeProto.RouteListRespMsg.newBuilder();

        if(context.getRet() == 0){
            UnionTradeProto.RouteListCrossReqMsg reqMsg = UnionTradeProto.RouteListCrossReqMsg.parseFrom(packet.getBytes());
            int routeId = reqMsg.getRouteId();
            List<Integer> hadSendList = new ArrayList<>(reqMsg.getSendListList());

            respMsg = CrossUnionTradeMgr.getNewestUnionRouteData(routeId, routeId != 0, context.getUnionInfo(), context.getMember());

            respMsg.setHadSendNum(hadSendList.size());
            respMsg.setServerOpenTime(CrossUnionTradeMgr.getServerOpenTime(context.getUnionInfo().getServerId()));
            respMsg.setRet(0);

            CrossUnionTradeMgr.resetData(context.getUnionInfo().getUnionUid());
        }else {
            respMsg.setRet(ret);
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_TRADE_ROUTE_LIST_DATA, respMsg));

    }

    @Override
    public void sendData() {

    }
}
